//
//  RowBaseCell.m
//
//  jhf
//
//  Created by Mac on 2020/7/17.
//  Copyright © 2020 jhfappkf@163.com. All rights reserved.
//

#import "RowBaseCell.h"

@implementation RowBaseCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self baseRowSetup];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self baseRowSetup];
    }
    return self;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self baseRowSetup];
        [self setup];
    }
    return self;
}

- (void)setup {}

- (void)baseRowSetup {
    [self.contentView addSubview:self.bgV];
    [self.bgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView).insets(UIEdgeInsetsZero);
    }];
    self.backgroundColor = [UIColor clearColor];
    
    [self.contentView addSubview:self.bottomLineV];
    [self.bottomLineV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.contentView).offset(0);
        make.height.mas_equalTo(0);
    }];
    
    [self.contentView sendSubviewToBack:self.bottomLineV];
    [self.contentView sendSubviewToBack:self.bgV];
}

- (void)bindTo:(__kindof id<BindProtocol>)model {
    self.model = model;
    BindModel *bind = (BindModel *)model;
    if (bind.model && [bind.model isKindOfClass:RowBaseM.class]) {
        RowBaseM *base = bind.model;
        [self.bgV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView).insets(base.bgVMarging);
        }];
        [base.bgControl controlV:self.bgV];
        [base.bottomLineControl controlV:self.bottomLineV];
        if (base.bottomLineControl.vHeightCons) {
            CGFloat leftCons = base.bottomLineControl.vLeftCons ?: 0;
            CGFloat rightCons = base.bottomLineControl.vRightCons ?: 0;
            [self.bottomLineV mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(base.bottomLineControl.vHeightCons);
                make.left.equalTo(self.contentView).offset(leftCons);
                make.right.equalTo(self.contentView).offset(rightCons);
            }];
        }
    }
}

- (UIView *)bgV {
    if (!_bgV) {
        _bgV = [UIView new];
    }
    return _bgV;
}

- (UIView *)bottomLineV {
    if (!_bottomLineV) {
        _bottomLineV = [UIView new];
    }
    return _bottomLineV;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
